import sys
from PySide6.QtWidgets import QWidget, QApplication, QVBoxLayout, QTreeWidget, QPushButton, QTreeWidgetItem, QLineEdit



class MyMainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setup_ui()


    def setup_ui(self):
        self.resize(800, 600)
        # 设置垂直布局
        v_box = QVBoxLayout(self)
        add_btn = QPushButton("增加")
        # 创建树形结构窗口
        tree_widgtes = QTreeWidget()
        # 设置树形结构的列数
        tree_widgtes.setColumnCount(1)
        #添加树形部件的列标签
        tree_widgtes.setHeaderLabels(["省"])


        # 添加输入框
        province_label = QLineEdit("省份")
        city_label = QLineEdit("市")
        area_label = QLineEdit("区县")

        # 在窗口上添加组件
        v_box.addWidget(tree_widgtes)
        v_box.addWidget(province_label)
        v_box.addWidget(city_label)
        # v_box.addWidget(area_label)
        v_box.addWidget(add_btn)


        self.tree_widgtes = tree_widgtes
        self.province = province_label
        self.city = city_label
        self.area = area_label

        # 给按钮绑定槽函数，实现添加功
        add_btn.clicked.connect(self.add_item)
    
    def add_item(self):
        """添加树形结构的值"""
        # 获取输入框中的值
        pro_text = self.province.text()
        city_text = self.city.text()
        area_text = self.area.text()

        top_item = QTreeWidgetItem(self.tree_widgtes)
        top_item.setText(0, pro_text)
        
        # top_item.setText(1, city_text)
        # top_item.setText(2, area_text)

        # # 给top的第一列或第二列添加值
        v_item = QTreeWidgetItem(top_item)
        # 树形结构同一列设置值会覆盖
        v_item.setText(0, city_text)
        v_item.setText(0, "荥阳")
  


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyMainWindow()
    window.show()
    sys.exit(app.exec())
